//
//  ZRTabBar.m
//  SinaByOC
//
//  Created by 张锐 on 16/2/27.
//  Copyright © 2016年 zhangr. All rights reserved.
//

#import "ZRTabBar.h"

@interface ZRTabBar ()

@property(nonatomic,strong) UIButton * centerButton;

@end

@implementation ZRTabBar

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self setupUI];
    }
    
    return self;


}
/**
 *  更新UI
 */
-(void)setupUI{
    
//    [self addCenterButton];
    
     [self addSubview:self.centerButton];


}
/**
 *  添加中心的按钮
 */
-(void)addCenterButton{


//    UIButton *centerButton = [[UIButton alloc]init];
//    
//    CGFloat width = [UIScreen mainScreen].bounds.size.width/5;
//    
//    CGFloat height = self.bounds.size.height;
//    
//    centerButton.frame = CGRectMake(2 * width, 0, width, height);
//    
//    [centerButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
//    
//    [centerButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
//    
//    [centerButton setBackgroundImage:[UIImage imageNamed: @"tabbar_compose_button" ]forState:UIControlStateNormal];
//    
//    [centerButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
//    
//    [centerButton addTarget:self action:@selector(centerButtonClick) forControlEvents:UIControlEventTouchUpInside];
//
//
//    [self addSubview:centerButton];
    
}
/**
 *  按钮懒加载
 */
-(UIButton *)centerButton{

    if (_centerButton == nil) {
        
        _centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_centerButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        
        [_centerButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [_centerButton setBackgroundImage:[UIImage imageNamed: @"tabbar_compose_button" ]forState:UIControlStateNormal];
        
        [_centerButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [_centerButton addTarget:self action:@selector(centerButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return _centerButton;

}

/**
 *  按钮点击事件
 */
-(void)centerButtonClick{

    [self.delegate compuseButtonClick];

}
/**
 *  重写layoutSubviews
 */
-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    int index = 0;
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width/5;
    
    CGFloat height = self.bounds.size.height;
    
    for (UIView *subview in self.subviews) {
        
        if ([subview isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            if (index == 2) {
                
                self.centerButton.frame = CGRectMake(index++ * width, 0, width, height);
//                index ++ ;
            }
            
            subview.frame = CGRectMake((index++)*width, 0, width, height);
            
        }
        
    }
}

@end
